//
//  ReportCardVC.m
//  jzyParents
//
//  Created by 任辰东 on 2022/8/22.
//

#import "ReportCardVC.h"
#import "ReportCardITotalView.h"
#import "ReportCardItemView.h"
#import "ReportCardScreenView.h"
#import "ReportCardDataItemView.h"
#import "ReportCardProblemListVC.h"
#import "ReportApis.h"
#import "KemuDetailPageVC.h"
#import "StudyReportScoreListView.h"

#import "KemuDetailPageVC.h"
#import "ReportCradAnswerSheePageVC.h"

@interface ReportCardVC ()

@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) ReportCardITotalView *totalView;
@property (nonatomic, strong) StudyReportScoreListView *scoreListView;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UIView *markView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) ReportCardScreenView *screenView;
@property (nonatomic, strong) ReportCardDataItemView *item0;
@property (nonatomic, strong) UIView *dataView;

@property (nonatomic, strong) ReportCardModel *model;
@property (nonatomic, assign) NSInteger screenType;

@property (nonatomic, strong) ReportCardDataModel *dataModel;

@end

@implementation ReportCardVC

-(instancetype)initWithModel:(ReportCardModel *)model
{
    self = [super init];
    if (self) {
        _model = model;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = self.model.name;
    
    if (self.model.is_liankao.boolValue) {
        self.screenType = 3;
    }else{
        self.screenType = 2;
    }
    
    [self getData];
    
    WeakSelf
    [self.scoreListView setScoreBtnBLock:^(Kaoshi_score_listItem * _Nonnull model) {
            
        StrongSelf
        KemuDetailPageVC *problemVC = [[KemuDetailPageVC alloc]initWithModel:self.model WithKemuID:model.kemu_id];
        [self.navigationController pushViewController:problemVC animated:YES];
    }];
    
    [self.scoreListView setCardBtnBLock:^(Kaoshi_score_listItem * _Nonnull model) {
        
        StrongSelf
        ReportCradAnswerSheePageVC *problemVC = [[ReportCradAnswerSheePageVC alloc]initWithModel:self.model WithKemuID:model.kemu_id];
        [self.navigationController pushViewController:problemVC animated:YES];
    }];
}

- (void)getData
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"main_id"] = self.model.main_id;
    params[@"cjdb_type"] = @(self.screenType);
    params[@"id_card"] = [GVUserDefaults standardUserDefaults].id_card;
//    params[@"id_card"] = @"410802200912100020";
//    params[@"main_id"] = @"333";
//    params[@"cjdb_type"] = @"2";
    
    WeakSelf
    [self showLoadingHUDWithMessage:nil];
    [ReportApis ctb_Xq_QuankeChengjiPageRequestWithParams:params SuccessBlock:^(NSNumber *code, NSString *msg, id responseData) {
        StrongSelf
        [self hideLoadingHUD];
        ReportCardDataModel *model = [ReportCardDataModel mj_objectWithKeyValues:responseData];
        self.dataModel = model;
        [self configView:model];
        
    } FailureBlock:^(NSNumber *code, NSString *msg) {
        StrongSelf
        [self showWarningHUDWithMessage:msg completion:NULL];
    }];
}

- (void)configView:(ReportCardDataModel *)model
{
    [self.totalView configViewWithModel:model.kaoshi_zong_score];
    [self.scoreListView configViewWithData:model.kaoshi_score_list];
    
    self.titleLabel.text = [NSString stringWithFormat:@"成绩对比(%@人)",model.duibi_num];
    
    [self.dataView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    ReportCardDataItemView *bufferDataItem;
    for (NSInteger i = 0; i < model.chengji_duibi_list.count; i ++) {
        
        ReportCardDataItemView *item = [ReportCardDataItemView new];
        [item configViewWithModel:model.chengji_duibi_list[i]];
        item.tag = i;
        [self.dataView addSubview:item];
//        [item addTarget:self action:@selector(itemAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [item mas_makeConstraints:^(MASConstraintMaker *make) {
            
            if (bufferDataItem) {
                make.top.equalTo(bufferDataItem.mas_bottom);
            }else{
                make.top.equalTo(self.dataView);
            }
            make.left.right.equalTo(self.dataView);
            
            if (i == model.chengji_duibi_list.count - 1) {
                make.bottom.equalTo(self.dataView.mas_bottom);
            }
        }];
        
        bufferDataItem = item;
    }
    
    [self.bgScrollView layoutIfNeeded];
    
}

- (void)itemAction:(ReportCardItemView *)item
{
    Kaoshi_score_listItem *model = self.dataModel.kaoshi_score_list[item.tag];
    KemuDetailPageVC *problemVC = [[KemuDetailPageVC alloc]initWithModel:self.model WithKemuID:model.kemu_id];
    [self.navigationController pushViewController:problemVC animated:YES];
}

-(void)setupViews
{
    [self.bgScrollView.contentView addSubview:self.topView];
    [self.topView addSubview:self.totalView];
    [self.topView addSubview:self.scoreListView];
    [self.bgScrollView.contentView addSubview:self.bottomView];
    [self.bottomView addSubview:self.markView];
    [self.bottomView addSubview:self.titleLabel];
//    [self.bottomView addSubview:self.screenView];
   
    [self.bottomView addSubview:self.item0];
    Chengji_duibi_listItem *model = [Chengji_duibi_listItem new];
    model.type = @"1";
    model.kemu_name = @"科目";
    model.score = @"分数";
    model.average_score = @"平均分";
    model.top_score = @"最高分";
    model.gap_score = @"差距分";
    [self.item0 configViewWithModel:model];
    [self.bottomView addSubview:self.dataView];
    
//    WeakSelf
//    [self.screenView setScreenBlock:^(NSInteger type) {
//            StrongSelf
//        self.screenType = type;
//        [self getData];
//    }];
}

-(void)setupLayout
{
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.bgScrollView.contentView);
        make.left.right.equalTo(self.bgScrollView.contentView);
    }];
    
    [self.totalView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.topView).offset(0);
        make.left.equalTo(self.topView).offset(0);
        make.right.equalTo(self.topView).offset(0);
    }];
    
    [self.scoreListView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.totalView.mas_bottom).offset(15);
        make.left.equalTo(self.topView).offset(0);
        make.right.equalTo(self.topView).offset(0);
        make.bottom.equalTo(self.topView).offset(-kOnePixel*2);
    }];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.topView.mas_bottom).offset(10);
        make.bottom.equalTo(self.bgScrollView.contentView).offset(kBottomOffset);
        make.left.right.equalTo(self.bgScrollView.contentView);
    }];
    
    [self.markView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.bottomView).offset(20);
        make.left.equalTo(self.bottomView).offset(15);
        make.height.mas_equalTo(13);
        make.width.mas_equalTo(4);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.equalTo(self.markView);
        make.left.equalTo(self.markView.mas_right).offset(8);
        make.right.equalTo(self.bottomView).offset(-15);
    }];
    
//    [self.screenView mas_makeConstraints:^(MASConstraintMaker *make) {
//       
//        make.centerY.equalTo(self.markView);
//        make.left.equalTo(self.titleLabel.mas_right).offset(8);
//        make.right.equalTo(self.bottomView).offset(-15);
//        make.width.mas_equalTo(120 + kOnePixel *2);
//    }];
    
    [self.item0 mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.markView.mas_bottom).offset(5);
        make.left.right.equalTo(self.bottomView);
    }];
    
    [self.dataView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(self.item0.mas_bottom).offset(0);
        make.left.right.equalTo(self.bottomView);
        make.bottom.equalTo(self.bottomView).offset(-15);
    }];
}


#pragma mark -- lazyload

-(UIView *)topView
{
    if (!_topView) {
        _topView = [UIView new];
        _topView.backgroundColor = UIColorFromHex(0xffffff);
        [_topView addBottomLine];
    }
    return _topView;
}

-(ReportCardITotalView *)totalView
{
    if (!_totalView) {
        _totalView = [ReportCardITotalView new];
    }
    return _totalView;
}

-(StudyReportScoreListView *)scoreListView
{
    if (!_scoreListView) {
        _scoreListView = [StudyReportScoreListView new];
    }
    return _scoreListView;
}

-(UIView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [UIView new];
        _bottomView.backgroundColor = UIColorFromHex(0xffffff);
        [_bottomView addTopLine];
        [_bottomView addBottomLine];
    }
    return _bottomView;
}

-(UIView *)markView
{
    if (!_markView) {
        _markView = [UIView new];
        _markView.backgroundColor = UIColorFromHex(kColorMain);
    }
    return _markView;
}


-(UIView *)dataView
{
    if (!_dataView) {
        _dataView = [UIView new];
        _dataView.backgroundColor = UIColorFromHex(kColorWhite);
    }
    return _dataView;
}

-(UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.textColor = UIColorFromHex(0x000000);
        _titleLabel.font = UIFontSize(14);
    }
    return _titleLabel;
}

-(ReportCardScreenView *)screenView
{
    if (!_screenView) {
        _screenView = [ReportCardScreenView new];
    }
    return _screenView;
}

-(ReportCardDataItemView *)item0
{
    if (!_item0) {
        _item0 = [ReportCardDataItemView new];
    }
    return _item0;
}
@end
